package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.User;
import com.gxwzxy.medic.entity.otherentity.UserRole;
import com.gxwzxy.medic.service.UserService;
import com.gxwzxy.medic.service.Impl.UserServiceImpl;
import com.gxwzxy.medic.until.PageBean;
import com.my.web.servlet.RequestBeanUtils;
 
/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends  BaseServlet {
	private static final long serialVersionUID = 1L;
       
   
	/**
	 * 鐢ㄦ埛鍒嗛〉灞曠ず
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findUser (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
//		UserService us = new UserServiceImpl();
//		List<User> lu = us.findUser(); 
		//获取前端传来的index
    	String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
    	
    	PageBean<User> pu = new PageBean<User>();
    	pu.setIndex(index);//璁剧疆灞曠ず鐨勫綋鍓嶉〉鏁�
    	UserService us = new UserServiceImpl();
    	us.pageUser(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/user/findUser.jsp").forward(request, response);

	}
	
	/**
	 * 鏍规嵁Id鍒犻櫎鐢ㄦ埛
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void UserDelete (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//鑾峰彇鍝嶅簲鏁版嵁
		User user = RequestBeanUtils.requestToBean(request, User.class);
		//澶勭悊璇锋眰淇℃伅
		UserService us = new UserServiceImpl();
		boolean bl = us.UserDelete(user.getUserId());
		if(bl) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
	
	/**
	 * 鏌ヨ鐢ㄦ埛缂栧彿鏄惁宸插瓨鍦�
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findUserId (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//鑾峰彇鍝嶅簲鏁版嵁
		User user = RequestBeanUtils.requestToBean(request, User.class);
		
		UserService us = new UserServiceImpl();
		boolean bl = us.UserfindUserId(user.getUserId());
		//濡傛灉缂栧彿瀛樺湪锛岃椤甸潰寰楀埌鐨刡l涓簍rue
		if(bl) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
	
	/**
	 * 鏌ヨ鐢ㄦ埛璐﹀彿鏄惁宸插瓨鍦�
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findUserName (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//鑾峰彇鍝嶅簲鏁版嵁
		User user = RequestBeanUtils.requestToBean(request, User.class);
		
		UserService us = new UserServiceImpl();
		boolean bl = us.UserfindUserName(user.getUserName());
		//濡傛灉璐﹀彿瀛樺湪锛岃椤甸潰寰楀埌鐨刡l涓簍rue
		if(bl) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
	/**
	 * 鍒涘缓鐢ㄦ埛
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void saveUser(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		//构造并拿到前端传来的值
		User user =RequestBeanUtils.requestToBean(request, User.class);
		String roleId = request.getParameter("roleId");
		UserService us = new UserServiceImpl();
		boolean bl = us.saveUser(user,roleId);
		if(bl) {
			request.setAttribute("url", "UserServlet?method=findUser&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	/**
	 * 鏍规嵁鐢ㄦ埛缂栧彿userId鏌ヨ鐢ㄦ埛淇℃伅
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findUserById (HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		//构造bean
		User user =RequestBeanUtils.requestToBean(request, User.class);
		UserService us = new UserServiceImpl();
		List<UserRole> lu = us.findUserById(user.getUserId()); 
		request.setAttribute("lu",lu);
		request.getRequestDispatcher("page/user/updateUser.jsp").forward(request, response);;
	}
	
	/**
	 * 鏍规嵁鐢ㄦ埛缂栧彿userId淇敼鐢ㄦ埛淇℃伅
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateUserById(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		//鑾峰彇鍝嶅簲鏁版嵁
		User user =RequestBeanUtils.requestToBean(request, User.class);
		String roleId = null;
		String uroleId = request.getParameter("roleId");//鍘熸潵鐨剅oleId
		String reroleId = request.getParameter("ReroleId");//鏇存敼鍚庣殑roleId
		//鍒ゆ柇浼犲洖鏉ョ殑role鏄惁涓烘洿鏀圭殑锛岃嫢rerole涓�0锛屽垯琛ㄧず涓嶆洿鏀癸紝鍒欒幏鍙栧師鏉ョ殑roleId
		if(reroleId.equals("0")) {
			roleId = uroleId;
		}else {
			roleId = reroleId;
		}
		UserService us = new UserServiceImpl();
		boolean bl = us.updateUser(user,roleId);
		if(bl) {
			request.setAttribute("url", "UserServlet?method=findUser&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
}
